package controllers;

import beans.InhoudBean1;
import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.io.*;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.*;
import javax.servlet.http.*;
import models.User;
import models.Course;
import models.CombiDeal;

public class CombinationDealController extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Dbmanager dbManager = new Dbmanager();
        dbManager.openConnection();
        QueryManager queryManager = new QueryManager(dbManager);
        Course course = new Course();
        LinkedList<Course> combidealcourses = new LinkedList<Course>();

        if (request.getParameter("course1") == null) {
            LinkedList<CombiDeal> combideals = queryManager.getAllCombiDeals();
            
            LinkedList<String> combidealsforpage = new LinkedList<String>();
            
            

            CombiDeal combi = combideals.get(0);
            String href = "combination_deals-confirm?course1=" + combi.getCourse1() + "&course2=" + combi.getCourse2() + "&course3=" + combi.getCourse3() + "&course4=" + combi.getCourse4() + "&combiPrice=" + combi.getCombidealprice();
            request.setAttribute("combidealhref1", href);
            combidealsforpage.add(href);

            combi = combideals.get(1);
            href = "combination_deals-confirm?course1=" + combi.getCourse1() + "&course2=" + combi.getCourse2() + "&course3=" + combi.getCourse3() + "&course4=" + combi.getCourse4() + "&combiPrice=" + combi.getCombidealprice();
            request.setAttribute("combidealhref2", href);
            combidealsforpage.add(href);

            combi = combideals.get(2);
            href = "combination_deals-confirm?course1=" + combi.getCourse1() + "&course2=" + combi.getCourse2() + "&course3=" + combi.getCourse3() + "&course4=" + combi.getCourse4() + "&combiPrice=" + combi.getCombidealprice();
            request.setAttribute("combidealhref3", href);
            combidealsforpage.add(href);

            combi = combideals.get(3);
            href = "combination_deals-confirm?course1=" + combi.getCourse1() + "&course2=" + combi.getCourse2() + "&course3=" + combi.getCourse3() + "&course4=" + combi.getCourse4() + "&combiPrice=" + combi.getCombidealprice();
            request.setAttribute("combidealhref4", href);
            combidealsforpage.add(href);

            dbManager.closeConnection();

            RequestDispatcher dispatcher = request.getRequestDispatcher("/combination_deals.jsp");
            dispatcher.forward(request, response);
        } else {
            double totalprice = 0.0;
            double totalprice2 = 0.0;


            if (request.getParameter("course1") != null && Integer.parseInt(request.getParameter("course1")) != 0) {
                course = queryManager.getCoursbyid(Integer.parseInt(request.getParameter("course1")));
                combidealcourses.add(course);
                totalprice += Double.valueOf(course.getPrice());
            }

            if (request.getParameter("course2") != null && Integer.parseInt(request.getParameter("course2")) != 0) {
                course = queryManager.getCoursbyid(Integer.parseInt(request.getParameter("course2")));
                combidealcourses.add(course);
                totalprice += Double.valueOf(course.getPrice());
            }

            if (request.getParameter("course3") != null && Integer.parseInt(request.getParameter("course3")) != 0) {
                course = queryManager.getCoursbyid(Integer.parseInt(request.getParameter("course3")));
                combidealcourses.add(course);
                totalprice += Double.valueOf(course.getPrice());
            }

            if (request.getParameter("course4") != null && Integer.parseInt(request.getParameter("course4")) != 0) {
                course = queryManager.getCoursbyid(Integer.parseInt(request.getParameter("course4")));
                combidealcourses.add(course);
                totalprice += Double.valueOf(course.getPrice());
            }

            if (request.getParameter("combiPrice") != null) {
                totalprice2 = Double.parseDouble(request.getParameter("combiPrice"));
            }

            InhoudBean1 inhoud;
            HttpSession sessie = request.getSession();
            inhoud = (InhoudBean1) sessie.getAttribute("inhoud");
            String totalPriceS = "";
            String totalPriceS2 = "";
            try {
                totalPriceS = inhoud.getAmount(totalprice);
                totalPriceS2 = inhoud.getAmount(totalprice2);
            } catch (ParseException ex) {
                Logger.getLogger(CombinationDealController.class.getName()).log(Level.SEVERE, null, ex);
            }
            request.setAttribute("combicourseinfo", combidealcourses);
            request.setAttribute("totalprice", totalPriceS);
            request.setAttribute("combiprice", totalPriceS2);
            request.setAttribute("course1", request.getParameter("course1"));
            request.setAttribute("course2", request.getParameter("course2"));
            request.setAttribute("course3", request.getParameter("course3"));
            request.setAttribute("course4", request.getParameter("course4"));

            doPost(request, response);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Dbmanager dbManager = new Dbmanager();
        dbManager.openConnection();
        QueryManager queryManager = new QueryManager(dbManager);
        HttpSession sessie = request.getSession();

        User user = (User) sessie.getAttribute("gebruikerinfo");
        request.setAttribute("gebruikerinfo", user);

        request.setAttribute("combicourseinfo", (LinkedList<Course>) request.getAttribute("combicourseinfo"));

        dbManager.closeConnection();
        RequestDispatcher dispatcher = request.getRequestDispatcher("/subscribe_confirmation.jsp");
        dispatcher.forward(request, response);
    }
}
